import {selectAll} from "./fake-db"
import createHandler from "./request"
import {getLoggedUser} from "./auth"

function existName(items, name) {
    if (!items) return false
    return items.find(item => item.name === name) !== undefined
}

const getCurMenu = createHandler(() => {
    const curUser = getLoggedUser()
    if (!curUser) return {
        code: 101,
        msg: '登录过期'
    }
    const role = curUser.role
    let menu = selectAll('menu')

    function filter(items) {
        return items
            .filter(item => item.show && item.auth.includes(role))
            .map(item => {
                const newItem = {
                    label: item.label,
                    name: item.name,
                    icon: item.icon
                }
                if (item.children) {
                    newItem.children = filter(item.children)
                }
                return newItem
            })
    }

    return {
        code: 0,
        data: filter(menu)
    }
})
export {getCurMenu}

function findItemByNamePath(namePath) {
    let menu = selectAll('menu')
    let item, brothers, index

    if (namePath.length === 0) {
        item = {children: menu}
        brothers = [item]
        index = 0
        return {item, brothers, index}
    }

    for (const name of namePath) {
        if (!menu) return {index: -1}
        index = menu.findIndex(item => item.name === name)
        if (index === -1) return {index: -1}
        item = menu[index]
        brothers = menu
        menu = item.children
    }
    return {item, brothers, index}
}

const changeMenuItem = createHandler((namePath, label, name, icon) => {
    const {item, brothers} = findItemByNamePath(namePath)
    if (!item) return {
        code: 1,
        msg: '未知错误'
    }

    if (item.name === name) {
        item.label = label
        item.icon = icon
    } else if (existName(brothers, name)) {
        return {
            code: 2001,
            msg: '前端名称重复，修改失败'
        }
    } else {
        item.name = name
        item.label = label
        item.icon = icon
    }

    return {
        code: 0
    }
})
export {changeMenuItem}

const changeMenuShow = createHandler((namePath, value) => {
    const {item} = findItemByNamePath(namePath)
    if (!item) return {
        code: 1,
        msg: '未知错误'
    }

    item.show = value
    return {
        code: 0
    }
})
export {changeMenuShow}

const getAllMenu = createHandler(() => {
    return {
        code: 0,
        data: selectAll('menu')
    }
})
export {getAllMenu}

const createMenuItem = createHandler((namePath, label, name, icon) => {
    const parent = findItemByNamePath(namePath).item
    if (!parent) return {
        code: 1,
        msg: '未知错误'
    }

    if (existName(parent.children, name)) return {
        code: 2002,
        msg: '前端名称已存在'
    }

    const newItem = {
        label,
        name,
        icon,
        show: true,
        auth: selectAll('role').map(item => item.name)
    }

    if (!parent.children) {
        parent.children = [newItem]
    } else {
        parent.children.push(newItem)
    }

    return {
        code: 0
    }
})
export {createMenuItem}

const deleteMenuItem = createHandler((namePath) => {
    const {brothers, index} = findItemByNamePath(namePath)
    if (index === -1) return {
        code: 1,
        msg: '未知错误'
    }
    brothers.splice(index, 1)
    return {
        code: 0
    }
})
export {deleteMenuItem}

function setMenuAuth(role, tarMenu, permitMenu) {
    const name2Item = {}
    for (let item of tarMenu) {
        name2Item[item.name] = item
    }
    for (let item of permitMenu) {
        const target = name2Item[item.name]
        if (!target) continue
        const index = target.auth.indexOf(role)
        if (item.permit && index === -1) {
            target.auth.push(role)
        } else if (!item.permit && index !== -1) {
            target.auth.splice(index, 1)
        }
        if (target.children && item.children) {
            setMenuAuth(role, target.children, item.children)
        }
    }
}

const allocateMenuForRole = createHandler((role, permitMenu) => {
    setMenuAuth(role, selectAll('menu'), permitMenu)
    return {
        code: 0
    }
})
export {allocateMenuForRole}

const moveUp = createHandler(namePath => {
    const {brothers, index} = findItemByNamePath(namePath)
    if (index === -1) return {
        code: 1,
        msg: '未知错误'
    }
    if (index === 0) return {
        code: 2003,
        msg: '已经是第一项了'
    }
    const temp = brothers[index - 1]
    brothers[index - 1] = brothers[index]
    brothers[index] = temp
    return {
        code: 0
    }
})
export {moveUp}

const moveDown = createHandler(namePath => {
    const {brothers, index} = findItemByNamePath(namePath)
    if (index === -1) return {
        code: 1,
        msg: '未知错误'
    }
    if (index === brothers.length - 1) return {
        code: 2004,
        msg: '已经是最后一项了'
    }
    const temp = brothers[index + 1]
    brothers[index + 1] = brothers[index]
    brothers[index] = temp
    return {
        code: 0
    }
})
export {moveDown}